package com.leanyuan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.domain.TCarinfo;
import com.leanyuan.publi.dao.OrdersMapper;
import com.leanyuan.publi.domain.Orders;
import com.leanyuan.service.TCarinfoService_V;
import com.leanyuan.service.TParkingspaceService_V;
import com.leanyuan.service.UserinfoService_V;
import com.leanyuan.vo.CarInfoVO;
import com.leanyuan.vo.CarUserInfoVo;
import com.leanyuan.vo.CarUserVo;
import com.leanyuan.vo.ParkingspaceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 相亲相爱一家人
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/leanyuan/t-carinfo")
public class TCarinfoController {
    @Autowired
    TCarinfoService_V tCarinfoServiceV;

    @Autowired
    TParkingspaceService_V tParkingspaceServiceV;

    @Autowired
    UserinfoService_V userinfoServiceV;

    @Autowired
    OrdersMapper ordersMapper;
    //查询所有业主车辆信息并分页
    @RequestMapping("selectPage")
    public IPage<CarInfoVO> selectPage(Integer page, Integer pageSize, Model model){
        Page<CarInfoVO> iPage=new Page<CarInfoVO>(page,pageSize);
        IPage<CarInfoVO> CarInfoPage= this.tCarinfoServiceV.getCarInfoAll(iPage);
        model.addAttribute("CarInfoPage",CarInfoPage);
        return CarInfoPage;
    }

    //查询所有业主车辆信息并分页(带条件的)
    @RequestMapping("selectPageByParam")
    public IPage<CarInfoVO> selectPageByParam(String carNumber,String parkingSpaceNumber,String carType, Integer page, Integer pageSize, Model model){
        Page<CarInfoVO> iPage=new Page<CarInfoVO>(page,pageSize);
        IPage<CarInfoVO> CarInfoPageByParam= this.tCarinfoServiceV.getCarInfoByParam(iPage,carNumber,parkingSpaceNumber,carType);
        model.addAttribute("CarInfoPageByParam",CarInfoPageByParam);
        return CarInfoPageByParam;
    }

    //根据id修改信息
    @RequestMapping("updateCarInfoById")
    public boolean updateCarInfoById(@RequestBody TCarinfo tCarinfo){
        QueryWrapper<TCarinfo> queryWrapper =new QueryWrapper<TCarinfo>();
        queryWrapper.eq("car_number",tCarinfo.getCarNumber());
        TCarinfo carinfo =this.tCarinfoServiceV.getOne(queryWrapper);
        if(carinfo==null){
            return  this.tCarinfoServiceV.updateById(tCarinfo);
        }
        return false;
    }

    //根据ID删除
    @RequestMapping( "deleteCarInfoById")
    public boolean deleteParkingspaceById(Integer id){

        String state="空闲";
        TCarinfo tCarinfo=this.tCarinfoServiceV.getById(id);
        this.tParkingspaceServiceV.updateParkingspaceState(tCarinfo.getParkingspaceId(),state);
        return this.tCarinfoServiceV.removeById(id);
    }

    //带条件的查询分页(查停车位)
    @RequestMapping("selectParkingspace")
    public IPage<ParkingspaceVo> selectParkingspace(Integer page, Integer pageSize, Model model){
        //分页
        Page<ParkingspaceVo> iPage=new Page<ParkingspaceVo>(page,pageSize);

        IPage<ParkingspaceVo> tParkingspaceIPage3=this.tParkingspaceServiceV.findByPage2(iPage);
        model.addAttribute("tParkingspaceIPage3",tParkingspaceIPage3);
        return tParkingspaceIPage3;
    }

    //带条件查询分页(查停车位)
    @RequestMapping("selectCarUserByParam")
    public IPage<ParkingspaceVo> selectCarUserByParam(Integer page, Integer pageSize,
                                                 String selCarparkinglot,String selParkingSpaceNumber, Model model){
        //分页
        Page<ParkingspaceVo> iPage=new Page<ParkingspaceVo>(page,pageSize);

        IPage<ParkingspaceVo> carUserByParam=this.tParkingspaceServiceV.findPageByParam(iPage,selCarparkinglot,selParkingSpaceNumber);
        model.addAttribute("carUserByParam",carUserByParam);
        return carUserByParam;
    }

    //查询分页(查业主信息)
    @RequestMapping("selectCarUser")
    public IPage<CarUserVo> selectCarUser(Integer page, Integer pageSize, Model model){
        //分页
        Page<CarUserVo> iPage=new Page<CarUserVo>(page,pageSize);

        IPage<CarUserVo> tParkingspaceIPage4=this.userinfoServiceV.findUserByPage(iPage);
        model.addAttribute("tParkingspaceIPage4",tParkingspaceIPage4);
        return tParkingspaceIPage4;
    }

    //带条件查询分页(查业主信息)
    @RequestMapping("selectUserInfoByParam")
    public IPage<CarUserVo> selectUserInfoByParam(Integer page, Integer pageSize,
                                                      Integer tel,String selusername, Model model){
        //分页
        Page<CarUserVo> iPage=new Page<CarUserVo>(page,pageSize);

        IPage<CarUserVo> carUserInfoByParam=this.userinfoServiceV.findUserPageByParam(iPage,tel,selusername);
        model.addAttribute("carUserInfoByParam",carUserInfoByParam);
        return carUserInfoByParam;
    }


    //指定查询业主信息(包括楼栋编号和房屋编号))
    @RequestMapping("selectCarUserInfo")
    public List<CarUserInfoVo> selectCarUserInfo(Integer id, Model model){
        List<CarUserInfoVo> carUserInfo = this.userinfoServiceV.findUserList(id);
        model.addAttribute("CarUserInfo",carUserInfo);
        return carUserInfo;
    }

    //添加业主车辆信息
    @RequestMapping("addUserCar")
    public boolean addUserCar(@RequestBody TCarinfo tCarinfo){
        Orders orders=new Orders();
        orders.setPId(1);
        orders.setAddtime(new Date());
        orders.setStart(new Date());
        orders.setStatus("收费中状态");
        orders.setHouseId(tCarinfo.getHouseId());

        Integer orders1=this.ordersMapper.insert(orders);

        tCarinfo.setOrderId(orders.getOrderId());

        boolean car=this.tCarinfoServiceV.save(tCarinfo);
        if(car){
            String state="";
            if(tCarinfo.getCarNumberType().equals("出售车辆")){
                state="已售卖";
            }else if(tCarinfo.getCarNumberType().equals("月租车")){
                state="已出租";
            }
            this.tParkingspaceServiceV.updateParkingspaceState(tCarinfo.getParkingspaceId(),state);
            return car;
        }
        return car;
    }
}

